
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>集合 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="enum.html" />
    
    
    <link rel="prev" href="basics.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">第一部分：基础</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="basics.html">
            
                <a href="basics.html">
            
                    
                    基础
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3" data-path="collections.html">
            
                <a href="collections.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="enum.html">
            
                <a href="enum.html">
            
                    
                    Enum 模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="pattern-matching.html">
            
                <a href="pattern-matching.html">
            
                    
                    模式匹配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="control-structures.html">
            
                <a href="control-structures.html">
            
                    
                    控制语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="functions.html">
            
                <a href="functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="pipe-operator.html">
            
                <a href="pipe-operator.html">
            
                    
                    管道操作符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="modules.html">
            
                <a href="modules.html">
            
                    
                    模块(Module)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="mix.html">
            
                <a href="mix.html">
            
                    
                    Mix
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="sigils.html">
            
                <a href="sigils.html">
            
                    
                    魔符(Sigil)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="documentation.html">
            
                <a href="documentation.html">
            
                    
                    文档模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="testing.html">
            
                <a href="testing.html">
            
                    
                    测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="comprehensions.html">
            
                <a href="comprehensions.html">
            
                    
                    推导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="strings.html">
            
                <a href="strings.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="date-time.html">
            
                <a href="date-time.html">
            
                    
                    日期和时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="mix-tasks.html">
            
                <a href="mix-tasks.html">
            
                    
                    自定义Mix任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="iex-helpers.html">
            
                <a href="iex-helpers.html">
            
                    
                    IEx辅助函数
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第二部分：高级</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../advanced/erlang.html">
            
                <a href="../advanced/erlang.html">
            
                    
                    和Erlang互操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../advanced/error-handling.html">
            
                <a href="../advanced/error-handling.html">
            
                    
                    错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="../advanced/escripts.html">
            
                <a href="../advanced/escripts.html">
            
                    
                    可执行文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="../advanced/concurrency.html">
            
                <a href="../advanced/concurrency.html">
            
                    
                    并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="../advanced/otp-concurrency.html">
            
                <a href="../advanced/otp-concurrency.html">
            
                    
                    OTP并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="../advanced/otp-supervisors.html">
            
                <a href="../advanced/otp-supervisors.html">
            
                    
                    OTP Supervisors
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="../advanced/otp-distribution.html">
            
                <a href="../advanced/otp-distribution.html">
            
                    
                    OTP 分布式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="../advanced/metaprogramming.html">
            
                <a href="../advanced/metaprogramming.html">
            
                    
                    元编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="../advanced/umbrella-projects.html">
            
                <a href="../advanced/umbrella-projects.html">
            
                    
                    Umbrella Projects
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="../advanced/typespec.html">
            
                <a href="../advanced/typespec.html">
            
                    
                    Specifications and types
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.11" data-path="../advanced/behaviours.html">
            
                <a href="../advanced/behaviours.html">
            
                    
                    行为
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.12" data-path="../advanced/gen-stage.html">
            
                <a href="../advanced/gen-stage.html">
            
                    
                    GenStage
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.13" data-path="../advanced/protocols.html">
            
                <a href="../advanced/protocols.html">
            
                    
                    协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.14" data-path="../advanced/nerves.html">
            
                <a href="../advanced/nerves.html">
            
                    
                    Nerves
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第三部分：ECTO</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../ecto/basics.html">
            
                <a href="../ecto/basics.html">
            
                    
                    Basics
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../ecto/changesets.html">
            
                <a href="../ecto/changesets.html">
            
                    
                    Changesets
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../ecto/associations.html">
            
                <a href="../ecto/associations.html">
            
                    
                    关联关系
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../ecto/querying.html">
            
                <a href="../ecto/querying.html">
            
                    
                    查询
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第四部分：专题</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../specifics/plug.html">
            
                <a href="../specifics/plug.html">
            
                    
                    Plug
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="../specifics/eex.html">
            
                <a href="../specifics/eex.html">
            
                    
                    嵌入的 Elixir(EEx)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="../specifics/ets.html">
            
                <a href="../specifics/ets.html">
            
                    
                    Erlang 项式存储(ETS)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="../specifics/mnesia.html">
            
                <a href="../specifics/mnesia.html">
            
                    
                    Mnesia 数据库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="../specifics/debugging.html">
            
                <a href="../specifics/debugging.html">
            
                    
                    调试
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第五部分：程序库</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../libraries/guardian.html">
            
                <a href="../libraries/guardian.html">
            
                    
                    Guardian(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="../libraries/poolboy.html">
            
                <a href="../libraries/poolboy.html">
            
                    
                    Poolboy
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="../libraries/benchee.html">
            
                <a href="../libraries/benchee.html">
            
                    
                    Benchee
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="../libraries/bypass.html">
            
                <a href="../libraries/bypass.html">
            
                    
                    Bypass
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="../libraries/distillery.html">
            
                <a href="../libraries/distillery.html">
            
                    
                    Distillery(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="../libraries/stream-data.html">
            
                <a href="../libraries/stream-data.html">
            
                    
                    StreamData
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >集合</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <p>&#x5217;&#x8868;&#xFF08;list&#xFF09;&#x3001;&#x5143;&#x7EC4;&#xFF08;tuple&#xFF09;&#x3001;&#x5173;&#x952E;&#x5B57;&#x5217;&#x8868;&#xFF08;keyword list&#xFF09;&#x3001;&#x6620;&#x5C04;&#xFF08;map&#xFF09;&#x3002;</p>
<h2 id="&#x5217;&#x8868;&#xFF08;list&#xFF09;">&#x5217;&#x8868;&#xFF08;List&#xFF09;</h2>
<p>&#x5217;&#x8868;&#x662F;&#x503C;&#x7684;&#x7B80;&#x5355;&#x96C6;&#x5408;&#xFF0C;&#x53EF;&#x4EE5;&#x5305;&#x542B;&#x4E0D;&#x540C;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;&#xFF0C;&#x800C;&#x4E14;&#x4E5F;&#x53EF;&#x80FD;&#x5305;&#x542B;&#x76F8;&#x540C;&#x7684;&#x503C;&#x3002;</p>
<pre><code class="lang-elixir">iex&gt; [<span class="hljs-number">3.14</span>, <span class="hljs-symbol">:pie</span>, <span class="hljs-string">&quot;Apple&quot;</span>]
[<span class="hljs-number">3.14</span>, <span class="hljs-symbol">:pie</span>, <span class="hljs-string">&quot;Apple&quot;</span>]
</code></pre>
<p>Elixir &#x5185;&#x90E8;&#x7528;&#x94FE;&#x8868;&#x5B9E;&#x73B0;&#x5217;&#x8868;&#xFF0C;&#x83B7;&#x53D6;&#x5217;&#x8868;&#x957F;&#x5EA6;&#x662F; <code>O(n)</code> &#x7684;&#x64CD;&#x4F5C;&#x3002;&#x8FD9;&#x4E5F;&#x4EE3;&#x8868;&#x7740;&#xFF0C;&#x5728;&#x5217;&#x8868;&#x7684;&#x5934;&#x90E8;&#x63D2;&#x5165;&#x6BD4;&#x5728;&#x5C3E;&#x90E8;&#x63D2;&#x5165;&#x8981;&#x5FEB;&#x3002;</p>
<pre><code class="lang-elixir">iex&gt; list = [<span class="hljs-number">3.14</span>, <span class="hljs-symbol">:pie</span>, <span class="hljs-string">&quot;Apple&quot;</span>]
[<span class="hljs-number">3.14</span>, <span class="hljs-symbol">:pie</span>, <span class="hljs-string">&quot;Apple&quot;</span>]
iex&gt; [<span class="hljs-string">&quot;&#x3C0;&quot;</span> | list]
[<span class="hljs-string">&quot;&#x3C0;&quot;</span>, <span class="hljs-number">3.14</span>, <span class="hljs-symbol">:pie</span>, <span class="hljs-string">&quot;Apple&quot;</span>]
iex&gt; list ++ [<span class="hljs-string">&quot;Cherry&quot;</span>]
[<span class="hljs-number">3.14</span>, <span class="hljs-symbol">:pie</span>, <span class="hljs-string">&quot;Apple&quot;</span>, <span class="hljs-string">&quot;Cherry&quot;</span>]
</code></pre>
<h3 id="&#x5217;&#x8868;&#x62FC;&#x63A5;">&#x5217;&#x8868;&#x62FC;&#x63A5;</h3>
<p>&#x5217;&#x8868;&#x62FC;&#x63A5;&#x4F7F;&#x7528; <code>++/2</code> &#x64CD;&#x4F5C;&#x7B26;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>] ++ [<span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">1</span>]
[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">1</span>]
</code></pre>
<p>&#x5173;&#x4E8E;&#x4E0A;&#x9762;&#x4F7F;&#x7528;&#x5230;&#x7684; <code>++/2</code> &#x683C;&#x5F0F;&#x7684;&#x8BF4;&#x660E;&#xFF1A;&#x5728; Elixir &#x4E2D;&#xFF08;&#x4EE5;&#x53CA; Elixir &#x7684;&#x57FA;&#x7840;&#x8BED;&#x8A00; Erlang&#xFF09;&#xFF0C;&#x51FD;&#x6570;&#x548C;&#x64CD;&#x4F5C;&#x7B26;&#x7684;&#x540D;&#x5B57;&#x7531;&#x4E24;&#x90E8;&#x5206;&#x7EC4;&#x6210;&#xFF1A;&#x540D;&#x5B57;&#xFF08;&#x6BD4;&#x5982;&#x8FD9;&#x91CC;&#x7684; <code>++</code>&#xFF09;&#x548C;&#x5143;&#x6570;(arity)&#x3002;&#x5143;&#x6570;&#x662F; Elixir &#x548C; Erlang &#x4EE3;&#x7801;&#x975E;&#x5E38;&#x6838;&#x5FC3;&#x7684;&#x90E8;&#x5206;&#xFF0C;&#x5B83;&#x4EE3;&#x8868;&#x4E86;&#x7ED9;&#x5B9A;&#x51FD;&#x6570;&#x63A5;&#x53D7;&#x7684;&#x53C2;&#x6570;&#x4E2A;&#x6570;&#xFF08;&#x6BD4;&#x5982;&#x8FD9;&#x91CC;&#x7684; 2&#xFF09;&#xFF0C;&#x5143;&#x6570;&#x548C;&#x540D;&#x5B57;&#x4E4B;&#x95F4;&#x901A;&#x8FC7;&#x659C;&#x7EBF;&#x5206;&#x5272;&#x3002;&#x6211;&#x4EEC;&#x540E;&#x9762;&#x4F1A;&#x8BB2;&#x5230;&#x66F4;&#x591A;&#x8FD9;&#x65B9;&#x9762;&#x7684;&#x5185;&#x5BB9;&#xFF0C;&#x77E5;&#x9053;&#x8FD9;&#x4E9B;&#x5DF2;&#x7ECF;&#x80FD;&#x5E2E;&#x4F60;&#x7406;&#x89E3;&#x5B83;&#x7684;&#x542B;&#x4E49;&#x4E86;&#x3002;</p>
<h3 id="&#x5217;&#x8868;&#x51CF;&#x6CD5;">&#x5217;&#x8868;&#x51CF;&#x6CD5;</h3>
<p><code>--/2</code> &#x64CD;&#x4F5C;&#x7B26;&#x652F;&#x6301;&#x5217;&#x8868;&#x7684;&#x51CF;&#x6CD5;&#xFF0C;&#x800C;&#x4E14;&#x51CF;&#x53BB;&#x4E0D;&#x5B58;&#x5728;&#x7684;&#x503C;&#x4E5F;&#x662F;&#x5B89;&#x5168;&#x7684;&#x3002;</p>
<pre><code class="lang-elixir">iex&gt; [<span class="hljs-string">&quot;foo&quot;</span>, <span class="hljs-symbol">:bar</span>, <span class="hljs-number">42</span>] -- [<span class="hljs-number">42</span>, <span class="hljs-string">&quot;bar&quot;</span>]
[<span class="hljs-string">&quot;foo&quot;</span>, <span class="hljs-symbol">:bar</span>]
</code></pre>
<p>&#x503C;&#x5F97;&#x6CE8;&#x610F;&#x7684;&#x662F;&#xFF0C;&#x5982;&#x679C;&#x5217;&#x8868;&#x4E2D;&#x6709;&#x91CD;&#x590D;&#x7684;&#x503C;&#xFF0C;&#x5BF9;&#x4E8E;&#x53F3;&#x8FB9;&#x5217;&#x8868;&#x4E2D;&#x7684;&#x51FA;&#x73B0;&#x7684;&#x6BCF;&#x4E00;&#x4E2A;&#x503C;&#xFF0C;&#x53EA;&#x4F1A;&#x4ECE;&#x5DE6;&#x8FB9;&#x5217;&#x8868;&#xFF08;&#x88AB;&#x51CF;&#x5217;&#x8868;&#xFF09;&#x4E2D;&#x79FB;&#x9664;&#x4E00;&#x4E2A;&#x4E0E;&#x4E4B;&#x76F8;&#x7B49;&#x7684;&#x503C;&#xFF0C;&#x5373;&#x7B2C;&#x4E00;&#x4E2A;&#x51FA;&#x73B0;&#x7684;&#x503C;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>] -- [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">2</span>]
[<span class="hljs-number">2</span>, <span class="hljs-number">3</span>]
</code></pre>
<p><strong>&#x6CE8;&#x610F;&#xFF1A;</strong>&#x8FD9;&#x91CC;&#x6BD4;&#x8F83;&#x662F;&#x5426;&#x76F8;&#x540C;&#x4F7F;&#x7528;&#x7684;&#x662F;<a href=".#comparison">&#x4E25;&#x683C;&#x6BD4;&#x8F83;(strict comparison)</a>&#xFF0C;&#x8BF7;&#x53C2;&#x8003;&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; [<span class="hljs-number">2</span>] -- [<span class="hljs-number">2.0</span>]
[<span class="hljs-number">2</span>]
iex&gt; [<span class="hljs-number">2.0</span>] -- [<span class="hljs-number">2.0</span>]
[]
</code></pre>
<h3 id="&#x5934;&#x5C3E;&#xFF08;head--tail&#xFF09;">&#x5934;/&#x5C3E;&#xFF08;head / tail&#xFF09;</h3>
<p>&#x4F7F;&#x7528;&#x5217;&#x8868;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x7ECF;&#x5E38;&#x8981;&#x548C;&#x5217;&#x8868;&#x7684;&#x5934;&#xFF08;head&#xFF09;&#x548C;&#x5C3E;&#xFF08;tail&#xFF09;&#x6253;&#x4EA4;&#x9053;&#xFF1A;&#x5217;&#x8868;&#x7684;&#x5934;&#x90E8;&#x662F;&#x5217;&#x8868;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#xFF1B;&#x5C3E;&#x90E8;&#x662F;&#x9664;&#x53BB;&#x7B2C;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#x5269;&#x4E0B;&#x7684;&#x5217;&#x8868;&#x3002; Elixir &#x63D0;&#x4F9B;&#x4E86;&#x4E24;&#x4E2A;&#x51FD;&#x6570; <code>hd</code> &#x548C; <code>tl</code> &#x6765;&#x83B7;&#x53D6;&#x8FD9;&#x4E24;&#x4E2A;&#x90E8;&#x5206;&#x3002;</p>
<pre><code class="lang-elixir">iex&gt; hd [<span class="hljs-number">3.14</span>, <span class="hljs-symbol">:pie</span>, <span class="hljs-string">&quot;Apple&quot;</span>]
<span class="hljs-number">3.14</span>
iex&gt; tl [<span class="hljs-number">3.14</span>, <span class="hljs-symbol">:pie</span>, <span class="hljs-string">&quot;Apple&quot;</span>]
[<span class="hljs-symbol">:pie</span>, <span class="hljs-string">&quot;Apple&quot;</span>]
</code></pre>
<p>&#x9664;&#x4E86;&#x4E0A;&#x9762;&#x7684;&#x63D0;&#x5230;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x4F60;&#x8FD8;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<a href="../pattern-matching">&#x6A21;&#x5F0F;&#x5339;&#x914D;(pattern matching)</a> &#x548C; <code>|</code> &#x64CD;&#x4F5C;&#x7B26;&#x6765;&#x628A;&#x4E00;&#x4E2A;&#x5217;&#x8868;&#x5206;&#x6210;&#x5934;&#x5C3E;&#x4E24;&#x90E8;&#x5206;&#xFF1B;&#x6211;&#x4EEC;&#x5728;&#x540E;&#x9762;&#x7684;&#x6559;&#x7A0B;&#x4E2D;&#x8FD8;&#x4F1A;&#x770B;&#x5230;&#x8FD9;&#x79CD;&#x7528;&#x6CD5;&#x3002;</p>
<pre><code class="lang-elixir">iex&gt; [head | tail] = [<span class="hljs-number">3.14</span>, <span class="hljs-symbol">:pie</span>, <span class="hljs-string">&quot;Apple&quot;</span>]
[<span class="hljs-number">3.14</span>, <span class="hljs-symbol">:pie</span>, <span class="hljs-string">&quot;Apple&quot;</span>]
iex&gt; head
<span class="hljs-number">3.14</span>
iex&gt; tail
[<span class="hljs-symbol">:pie</span>, <span class="hljs-string">&quot;Apple&quot;</span>]
</code></pre>
<h2 id="&#x5143;&#x7EC4;&#xFF08;tuple&#xFF09;">&#x5143;&#x7EC4;&#xFF08;Tuple&#xFF09;</h2>
<p>&#x5143;&#x7EC4;&#x548C;&#x5217;&#x8868;&#x5F88;&#x50CF;&#xFF0C;&#x4F46;&#x662F;&#x5143;&#x7EC4;&#x5728;&#x5185;&#x5B58;&#x4E2D;&#x662F;&#x8FDE;&#x7EED;&#x5B58;&#x653E;&#x7684;&#x3002;&#x8FD9;&#x6837;&#x7684;&#x8BDD;&#xFF0C;&#x83B7;&#x53D6;&#x5143;&#x7EC4;&#x7684;&#x957F;&#x5EA6;&#x5F88;&#x5FEB;&#xFF0C;&#x4F46;&#x662F;&#x4FEE;&#x6539;&#x5143;&#x7EC4;&#x7684;&#x64CD;&#x4F5C;&#x5F88;&#x6602;&#x8D35;&#xFF1A;&#x65B0;&#x7684;&#x5143;&#x7EC4;&#x5FC5;&#x987B;&#x91CD;&#x65B0;&#x5728;&#x5185;&#x5B58;&#x4E2D;&#x62F7;&#x8D1D;&#x4E00;&#x4EFD;&#x3002;
&#x5B9A;&#x4E49;&#x5143;&#x7EC4;&#x8981;&#x7528;&#x82B1;&#x62EC;&#x53F7;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; {<span class="hljs-number">3.14</span>, <span class="hljs-symbol">:pie</span>, <span class="hljs-string">&quot;Apple&quot;</span>}
{<span class="hljs-number">3.14</span>, <span class="hljs-symbol">:pie</span>, <span class="hljs-string">&quot;Apple&quot;</span>}
</code></pre>
<p>&#x5143;&#x7EC4;&#x4E00;&#x4E2A;&#x5F88;&#x5E38;&#x89C1;&#x7684;&#x7528;&#x6CD5;&#x662F;&#x4F5C;&#x4E3A;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#xFF0C;&#x6765;&#x8FD4;&#x56DE;&#x989D;&#x5916;&#x7684;&#x4FE1;&#x606F;&#x3002;&#x5F53;&#x4ECB;&#x7ECD;&#x5230;<a href="../pattern-matching">&#x6A21;&#x5F0F;&#x5339;&#x914D;</a>&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x8FD9;&#x79CD;&#x7528;&#x6CD5;&#x7684;&#x597D;&#x5904;&#x5C31;&#x663E;&#x800C;&#x6613;&#x89C1;&#x4E86;&#x3002;</p>
<pre><code class="lang-elixir">iex&gt; File.read(<span class="hljs-string">&quot;path/to/existing/file&quot;</span>)
{<span class="hljs-symbol">:ok</span>, <span class="hljs-string">&quot;... contents ...&quot;</span>}
iex&gt; File.read(<span class="hljs-string">&quot;path/to/unknown/file&quot;</span>)
{<span class="hljs-symbol">:error</span>, <span class="hljs-symbol">:enoent</span>}
</code></pre>
<h2 id="&#x5173;&#x952E;&#x5B57;&#x5217;&#x8868;&#xFF08;keyword-list&#xFF09;">&#x5173;&#x952E;&#x5B57;&#x5217;&#x8868;&#xFF08;Keyword list&#xFF09;</h2>
<p>&#x5173;&#x952E;&#x5B57;&#x5217;&#x8868;&#xFF08;keywords&#xFF09;&#x548C;&#x6620;&#x5C04;&#xFF08;maps&#xFF09;&#x662F; Elixir &#x4E2D;&#x4E24;&#x4E2A;&#x76F8;&#x5173;&#x7684;&#x96C6;&#x5408;&#x3002;Elixir &#x7684;&#x5173;&#x952E;&#x5B57;&#x5217;&#x8868;&#x662F;&#x4E00;&#x79CD;&#x7279;&#x6B8A;&#x7684;&#x5217;&#x8868;&#xFF1A;&#x5217;&#x8868;&#x91CC;&#x7684;&#x5185;&#x5BB9;&#x662F;&#x4E8C;&#x5143;&#x5143;&#x7EC4;&#xFF0C;&#x5E76;&#x4E14;&#x4E8C;&#x5143;&#x7EC4;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#x5FC5;&#x987B;&#x662F;&#x539F;&#x5B50;&#x3002;&#x5B83;&#x548C;&#x5217;&#x8868;&#x7684;&#x884C;&#x4E3A;&#x5B8C;&#x5168;&#x4E00;&#x81F4;&#x3002;</p>
<pre><code class="lang-elixir">iex&gt; [<span class="hljs-symbol">foo:</span> <span class="hljs-string">&quot;bar&quot;</span>, <span class="hljs-symbol">hello:</span> <span class="hljs-string">&quot;world&quot;</span>]
[<span class="hljs-symbol">foo:</span> <span class="hljs-string">&quot;bar&quot;</span>, <span class="hljs-symbol">hello:</span> <span class="hljs-string">&quot;world&quot;</span>]
iex&gt; [{<span class="hljs-symbol">:foo</span>, <span class="hljs-string">&quot;bar&quot;</span>}, {<span class="hljs-symbol">:hello</span>, <span class="hljs-string">&quot;world&quot;</span>}]
[<span class="hljs-symbol">foo:</span> <span class="hljs-string">&quot;bar&quot;</span>, <span class="hljs-symbol">hello:</span> <span class="hljs-string">&quot;world&quot;</span>]
</code></pre>
<p>&#x5173;&#x952E;&#x5B57;&#x5217;&#x8868;&#x975E;&#x5E38;&#x91CD;&#x8981;&#xFF0C;&#x5B83;&#x6709;&#x4EE5;&#x4E0B;&#x7684;&#x7279;&#x6027;&#xFF1A;</p>
<ul>
<li>&#x952E;&#xFF08;key&#xFF09;&#x90FD;&#x662F;&#x539F;&#x5B50;&#xFF08;atom&#xFF09;</li>
<li>&#x952E;&#xFF08;key&#xFF09;&#x662F;&#x6709;&#x5E8F;&#x7684;&#xFF08;&#x5B9A;&#x4E49;&#x540E;&#xFF0C;&#x987A;&#x5E8F;&#x4E0D;&#x4F1A;&#x6539;&#x53D8;&#xFF09;</li>
<li>&#x952E;&#xFF08;key&#xFF09;&#x4E0D;&#x5FC5;&#x662F;&#x552F;&#x4E00;&#x7684;</li>
</ul>
<p>&#x56E0;&#x4E3A;&#x8FD9;&#x4E9B;&#x539F;&#x56E0;&#xFF0C;&#x5173;&#x952E;&#x5B57;&#x5217;&#x8868;&#x6700;&#x5E38;&#x89C1;&#x7684;&#x7528;&#x6CD5;&#x662F;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x4F20;&#x9012;&#x7ED9;&#x51FD;&#x6570;&#x3002;</p>
<h2 id="&#x6620;&#x5C04;&#xFF08;map&#xFF09;">&#x6620;&#x5C04;&#xFF08;Map&#xFF09;</h2>
<p>Elixir &#x7684;&#x6620;&#x5C04;&#xFF08;maps&#xFF09;&#x662F;&#x952E;&#x503C;&#x5BF9;&#x7ED3;&#x6784;&#x7684;&#x7B2C;&#x4E00;&#x9009;&#x62E9;&#xFF0C;&#x548C;&#x5173;&#x952E;&#x5B57;&#x5217;&#x8868;&#xFF08;keywords&#xFF09;&#x4E0D;&#x540C;&#xFF0C;&#x6620;&#x5C04;&#x5141;&#x8BB8;&#x4EFB;&#x610F;&#x7C7B;&#x578B;&#x7684;&#x6570;&#x636E;&#x4F5C;&#x4E3A;&#x952E;&#xFF0C;&#x800C;&#x4E14;&#x6570;&#x636E;&#x5E76;&#x4E0D;&#x4E25;&#x683C;&#x6392;&#x5E8F;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>%{}</code> &#x6765;&#x5B9A;&#x4E49;&#x6620;&#x5C04;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; map = %{<span class="hljs-symbol">:foo</span> =&gt; <span class="hljs-string">&quot;bar&quot;</span>, <span class="hljs-string">&quot;hello&quot;</span> =&gt; <span class="hljs-symbol">:world</span>}
%{<span class="hljs-symbol">:foo</span> =&gt; <span class="hljs-string">&quot;bar&quot;</span>, <span class="hljs-string">&quot;hello&quot;</span> =&gt; <span class="hljs-symbol">:world</span>}
iex&gt; map[<span class="hljs-symbol">:foo</span>]
<span class="hljs-string">&quot;bar&quot;</span>
iex&gt; map[<span class="hljs-string">&quot;hello&quot;</span>]
<span class="hljs-symbol">:world</span>
</code></pre>
<p>&#x4ECE; Elixir 1.2 &#x7248;&#x672C;&#x5F00;&#x59CB;&#xFF0C;&#x53D8;&#x91CF;&#x4E5F;&#x53EF;&#x4EE5;&#x4F5C;&#x4E3A;&#x6620;&#x5C04;&#x7684;&#x952E;&#xFF08;key&#xFF09;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; key = <span class="hljs-string">&quot;hello&quot;</span>
<span class="hljs-string">&quot;hello&quot;</span>
iex&gt; %{key =&gt; <span class="hljs-string">&quot;world&quot;</span>}
%{<span class="hljs-string">&quot;hello&quot;</span> =&gt; <span class="hljs-string">&quot;world&quot;</span>}
</code></pre>
<p>&#x5982;&#x679C;&#x91CD;&#x590D;&#x7684;&#x952E;&#x6DFB;&#x52A0;&#x5230;&#x6620;&#x5C04;&#x4E2D;&#xFF0C;&#x540E;&#x9762;&#x7684;&#x503C;&#x4F1A;&#x8986;&#x76D6;&#x4E4B;&#x524D;&#x7684;&#x503C;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; %{<span class="hljs-symbol">:foo</span> =&gt; <span class="hljs-string">&quot;bar&quot;</span>, <span class="hljs-symbol">:foo</span> =&gt; <span class="hljs-string">&quot;hello world&quot;</span>}
%{<span class="hljs-symbol">foo:</span> <span class="hljs-string">&quot;hello world&quot;</span>}
</code></pre>
<p>&#x4ECE;&#x4E0A;&#x9762;&#x7684;&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x4E2D;&#xFF0C;&#x4F60;&#x6216;&#x8BB8;&#x53D1;&#x73B0;&#x4E86;&#x4E00;&#x4E9B;&#x4E0D;&#x540C;&#xFF1A;&#x5B58;&#x50A8;&#x952E;&#x53EA;&#x6709;&#x539F;&#x5B50;&#x7684;&#x6620;&#x5C04;&#xFF0C;&#x53EF;&#x4EE5;&#x4E0D;&#x7528; <code>=&gt;</code>&#xFF0C;&#x76F4;&#x63A5;&#x4F7F;&#x7528;&#x5173;&#x952E;&#x5B57;&#x5217;&#x8868;&#x7684;&#x8BED;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; %{<span class="hljs-symbol">foo:</span> <span class="hljs-string">&quot;bar&quot;</span>, <span class="hljs-symbol">hello:</span> <span class="hljs-string">&quot;world&quot;</span>}
%{<span class="hljs-symbol">foo:</span> <span class="hljs-string">&quot;bar&quot;</span>, <span class="hljs-symbol">hello:</span> <span class="hljs-string">&quot;world&quot;</span>}

iex&gt; %{<span class="hljs-symbol">foo:</span> <span class="hljs-string">&quot;bar&quot;</span>, <span class="hljs-symbol">hello:</span> <span class="hljs-string">&quot;world&quot;</span>} == %{<span class="hljs-symbol">:foo</span> =&gt; <span class="hljs-string">&quot;bar&quot;</span>, <span class="hljs-symbol">:hello</span> =&gt; <span class="hljs-string">&quot;world&quot;</span>}
<span class="hljs-keyword">true</span>
</code></pre>
<p>&#x6620;&#x5C04;&#x53E6;&#x4E00;&#x4E2A;&#x6709;&#x8DA3;&#x7684;&#x7279;&#x6027;&#x662F;&#xFF1A;&#x5B83;&#x4EEC;&#x63D0;&#x4F9B;&#x4E86;&#x81EA;&#x5DF1;&#x66F4;&#x65B0;&#x548C;&#x83B7;&#x53D6;&#x539F;&#x5B50;&#x952E;&#xFF08;key&#xFF09;&#x7684;&#x8BED;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; map = %{<span class="hljs-symbol">foo:</span> <span class="hljs-string">&quot;bar&quot;</span>, <span class="hljs-symbol">hello:</span> <span class="hljs-string">&quot;world&quot;</span>}
%{<span class="hljs-symbol">foo:</span> <span class="hljs-string">&quot;bar&quot;</span>, <span class="hljs-symbol">hello:</span> <span class="hljs-string">&quot;world&quot;</span>}
iex&gt; %{map | <span class="hljs-symbol">foo:</span> <span class="hljs-string">&quot;baz&quot;</span>}
%{<span class="hljs-symbol">foo:</span> <span class="hljs-string">&quot;baz&quot;</span>, <span class="hljs-symbol">hello:</span> <span class="hljs-string">&quot;world&quot;</span>}
iex&gt; map.hello
<span class="hljs-string">&quot;world&quot;</span>
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="basics.html" class="navigation navigation-prev " aria-label="Previous page: 基础">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="enum.html" class="navigation navigation-next " aria-label="Next page: Enum 模块">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"version":"1.2.5","title":"集合","level":"1.3","depth":1,"next":{"title":"Enum 模块","level":"1.4","depth":1,"path":"lessons/basics/enum.md","ref":"lessons/basics/enum.md","articles":[]},"previous":{"title":"基础","level":"1.2","depth":1,"path":"lessons/basics/basics.md","ref":"lessons/basics/basics.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"lessons/basics/collections.md","mtime":"2019-11-05T02:25:51.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-11-05T03:01:22.004Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

